package org.lql.algo.codecrush.week010;

import org.lql.algo.common.TreeNode;

/**
 * @author: liangqinglong
 * @date: 2025-08-30 15:20
 * @description: 114. 二叉树展开为链表 <a href="https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/description/">...</a>
 **/
public class Flatten {

	public void flatten(TreeNode root) {
		TreeNode current = root;
		while (current != null) {
			if (current.left != null) {
				TreeNode next = current.left;
				TreeNode pre = next;
				// 遍历查找左子树的最右子节点
				while (pre.right != null) {
					pre = pre.right;
				}
				pre.right = current.right;
				// 将当前节点的左指针置为空
				current.left = null;
				// 将当前节点的右指针指向左子树
				current.right = next;
			}
			current = current.right;
		}
	}
}
